package com.shixianjun.leetcode;

public class _26_删除有序数组中的重复项 {

    static public int removeDuplicates(int[] nums) {

        if (nums.length <= 1) {
            return nums.length;
        }

        int currLength = 1;
        int p = nums[0];

        for (int i = 1; i < nums.length; i++) {
            if (nums[i] != p ) {
                p = nums[i];
                if (currLength != i) {
                    swap(nums, currLength, i);
                }
                currLength++;
            }
        }
        print(nums);
        return currLength;
    }

    static private void swap(int[] nums, int f, int t) {
        int temp = nums[f];
        nums[f] = nums[t];
        nums[t] = nums[f];
    }

    static private void print(int[] nums) {
        System.out.println("==============");
        for (int vaule : nums) {
            System.out.println(vaule);
        }
        System.out.println("==============");
    }

    public static void main(String[] args) {
        int[] nums = new int[] {1, 2};
        System.out.println(removeDuplicates(nums));
    }

}
